#include <cstring>
#include <iostream>
#include <cstdio>
using namespace std;
#define MAX_N 30005
int fa[MAX_N];
int rnk[MAX_N];
int num[MAX_N];
int Find(int x){
    int result = x;
    if (x!=fa[x]){
        result = Find(fa[x]);
        rnk[x] += rnk[fa[x]];
        fa[x] = result;
    }
    return result;
}


void Union(int x,int y){
    int tx = Find(x);
    int ty = Find(y);
    if (tx!=ty){
        fa[tx] = ty;
        rnk[tx] += num[ty];
        num[ty] += num[tx];
    }
}
/*
6
M 1 6
C 1
M 2 4
M 2 6
C 3
C 4
*/
int main(){
    //freopen("1988.txt","r",stdin);
    int P;
    char op[3];
    int X,Y;
    for (int i = 0 ; i < MAX_N ; i++)
    {
        fa[i] = i;
        num[i] = 1;
        rnk[i] = 0;
    }
    scanf("%d",&P);
    while (P--){
        scanf("%s",op);
        if(op[0]=='M'){
            scanf("%d%d",&X,&Y);
            Union(X,Y);
        }
        else {
            scanf("%d",&X);
            Find(X);
            printf("%d\n",rnk[X]);
        }
    }
    return 0;
}
